package enterpriseCustomer

import (
	"context"
	"github.com/google/uuid"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/customer/internal/svc"
	"insighttracking/apps/customer/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetEnterpriseCustomerBaseInfoApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetEnterpriseCustomerBaseInfoApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetEnterpriseCustomerBaseInfoApiLogic {
	hostname, _ := os.Hostname()
	return &GetEnterpriseCustomerBaseInfoApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetEnterpriseCustomerBaseInfoApiLogic) GetEnterpriseCustomerBaseInfoApi() (resp *types.EnterpriseCustomerResp, err error) {
	// todo: add your logic here and delete this line
	enterpriseId := cast.ToInt64(l.ctx.Value("enterpriseId"))
	customerId := cast.ToInt64(l.ctx.Value("customerId"))
	enterpriseCustomerEntity, err := dao.EnterpriseCustomerEntity.WithContext(l.ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(enterpriseId),
		dao.EnterpriseCustomerEntity.ID.Eq(customerId)).
		First()
	if err != nil {
		return nil, errors.WithMessage(err, "查询失败")
	}
	return &types.EnterpriseCustomerResp{
		ID:           enterpriseCustomerEntity.ID,                                 // 主键id
		Name:         enterpriseCustomerEntity.Name,                               // 门店名称
		Person:       enterpriseCustomerEntity.Person,                             // 负责人
		Mobile:       enterpriseCustomerEntity.Mobile,                             // 联系电话
		Logo:         enterpriseCustomerEntity.Logo,                               // 状态logo
		Status:       enterpriseCustomerEntity.Status,                             // 状态0：营业  1：停业
		Types:        enterpriseCustomerEntity.Types,                              // 客户类型0：公司，1：门店，2：个人
		ProvinceCode: enterpriseCustomerEntity.ProvinceCode,                       // 省份id
		CityCode:     enterpriseCustomerEntity.CityCode,                           // 城市id
		CountyCode:   enterpriseCustomerEntity.CountyCode,                         // 县、区id
		TownCode:     enterpriseCustomerEntity.TownCode,                           // 乡镇code
		Address:      enterpriseCustomerEntity.Address,                            // 具体地址
		FullAddress:  enterpriseCustomerEntity.FullAddress,                        // 拼接省市县地址
		CreatedAt:    utils.TimestampToString(enterpriseCustomerEntity.CreatedAt), // 创建时间
		UpdatedAt:    utils.TimestampToString(enterpriseCustomerEntity.UpdatedAt), // 更新时间
	}, nil
}
